package services

import (
	"360/video-srv/basic/config"
	__ "360/video-srv/basic/proto"
	"360/video-srv/modler"
	"context"
	"encoding/json"
)

func (s *Server) Hot(_ context.Context, in *__.HotReq) (*__.HotResp, error) {
	var video modler.Video
	config.DB.Debug().Where("id = ?", in.VideoId).Find(&video)
	var hots float64
	hots = 0.6*float64(video.PlayCount) + float64(video.LikeCount)*0.2 + float64(video.CollectCount)*0.1 + float64(video.CommentCount)*0.1
	config.DB.Debug().Model(&modler.Video{}).Where("id = ?", in.VideoId).Update("hot", hots)
	return &__.HotResp{
		Hot: float32(hots),
	}, nil
}
func (s *Server) HotList(_ context.Context, in *__.HotListReq) (*__.HotListResp, error) {
	var video []modler.Video
	config.DB.Debug().Limit(50).Order("hot desc").Find(&video)
	var b []*__.HotListItem
	for _, c := range video {
		list := __.HotListItem{
			VideoId:      int64(c.ID),
			Title:        c.Title,
			PlayCount:    int64(c.PlayCount),
			LikeCount:    int64(c.LikeCount),
			CollectCount: int64(c.CollectCount),
			CommentCount: int64(c.CommentCount),
			Hots:         float32(c.Hot),
		}
		b = append(b, &list)
	}
	marshal, _ := json.Marshal(&b)
	config.Rdb.Set(config.Ctx, "top50排行榜", marshal, 0)
	return &__.HotListResp{List: b}, nil
}
